package com.tiny.cqzcp.main.thread;

import com.tiny.cqzcp.pojo.klb.KlbOne;
import com.tiny.cqzcp.pojo.klb.KlbOneInfo;
import com.tiny.cqzcp.pojo.klb.Lottery;
import com.tiny.cqzcp.service.klb.KlbOneService;
import com.tiny.cqzcp.utils.ToolkitUtil;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.util.StringUtils;

public class KlbOneRunnable implements Runnable {

    private static final Logger log = LogManager.getLogger(KlbOneRunnable.class);
    
    private static final int SIZE = 10000;
    private static final String TEMP_TERM = "20001";

    private KlbOneService oneService;
    private List<Lottery> maxTerms;
    private List<Lottery> list;
    private Integer start;
    private Integer pageSize;

    public KlbOneRunnable(KlbOneService oneService,
                          List<Lottery> maxTerms,
                          List<Lottery> list,
                          Integer start,
                          Integer pageSize) {
        this.oneService = oneService;
        this.list = list;
        this.start = start;
        this.pageSize = pageSize;
        this.maxTerms = maxTerms;
    }

    @Override
    public void run() {
        try {
            List<KlbOneInfo> ntis = new ArrayList<>();
            List<KlbOne> nts = oneService.queryOneByLimit(start, pageSize);
            for (int n = list.size() - 1; n >= 0; n--) {
                Lottery lottery = list.get(n);
                String[] numbers = lottery.getLotteryRes().split(",");
                String term = lottery.getLotteryNo();
                for (KlbOne nt : nts) {
                    String one = nt.getOneNumbers().replaceAll(" ", "");
                    String lastTerm = nt.getOneLastTerm();
                    int miss = ToolkitUtil.getMissValue(lastTerm, term, TEMP_TERM, maxTerms);
                    HashSet<String> set = new HashSet<>(Arrays.asList(numbers));
                    set.retainAll(Arrays.asList(one.split(",")));
                    if (set.size() == 1) {
                        if(StringUtils.isEmpty(nt.getOneLastTerm())){
                            miss--;
                        }
                        if (nt.getOneMissMax() < miss) {
                            nt.setOneMissMax(miss);
                        }
                        nt.setOneLastTerm(term);
                        nt.setOneMiss(0);
                        nt.setOneRate("0");
                        nt.setOneLastMiss(miss);
                        KlbOneInfo nti = new KlbOneInfo();
                        nti.setOneId(nt.getOneId());
                        nti.setOneInfoTerm(term);
                        nti.setOneInfoMiss(miss);
                        ntis.add(nti);
                    } else {
                        nt.setOneMiss(miss);
                        NumberFormat numberFormat = NumberFormat.getInstance();
                        numberFormat.setMaximumFractionDigits(2);
                        String rate = numberFormat.format((double)miss / (double)4 * 100);
                        nt.setOneRate(rate);
                    }
                }
            }
            batchUpdate(nts);
            batchInsert(ntis);
        } catch (Exception e) {
            log.error(ToolkitUtil.printStackTraceToString(e));
        }
        log.info("快乐8一码-" + Thread.currentThread().getName() + "-完成！等待其他进程！");
    }

    private void batchUpdate(List<KlbOne> nfs) {
        int pageCount = 0;
        int m = nfs.size() % SIZE;
        if (m > 0) {
            pageCount = nfs.size() / SIZE + 1;
        } else {
            pageCount = nfs.size() / SIZE;
        }
        for (int i = 1; i <= pageCount; i++) {
            List<KlbOne> subList;
            if (m == 0) {
                subList = nfs.subList((i - 1) * SIZE, SIZE * (i));
            } else {
                if (i == pageCount) {
                    subList = nfs.subList((i - 1) * SIZE, nfs.size());
                } else {
                    subList = nfs.subList((i - 1) * SIZE, SIZE * (i));
                }
            }
            oneService.batchUpdate(subList);
        }
    }

    private void batchInsert(List<KlbOneInfo> ntis) {
        int pageCount = 0;
        int m = ntis.size() % SIZE;
        if (m > 0) {
            pageCount = ntis.size() / SIZE + 1;
        } else {
            pageCount = ntis.size() / SIZE;
        }
        for (int i = 1; i <= pageCount; i++) {
            List<KlbOneInfo> subList;
            if (m == 0) {
                subList = ntis.subList((i - 1) * SIZE, SIZE * (i));
            } else {
                if (i == pageCount) {
                    subList = ntis.subList((i - 1) * SIZE, ntis.size());
                } else {
                    subList = ntis.subList((i - 1) * SIZE, SIZE * (i));
                }
            }
            oneService.batchInsertInfo(subList);
        }
    }
}
